package com.exam.service;
import org.apache.struts2.ServletActionContext;

import com.exam.model.SettingModel;
import com.exam.util.Tools;

/**
 * 提供分页服务
 * @author 维康
 *
 */
public class PageService {
	private int onePageNum = 8;
	private int pageNow = 1;
	private int pageTotal; //总页数
	int count;	//总记录条数
	String url = "";
	
	private static int LR = 2; //当前页左右显示2个页面
	
	public PageService(int count , String url ,String pageNow){
		SettingModel sm= (SettingModel)ServletActionContext.getServletContext().getAttribute("SYS_SETTING");
		this.onePageNum = sm.getPageSize();
		this.url=url;
		this.init(count, pageNow);
	}
	public PageService(int count , String url ,String pageNow ,int onePageNum){
		this.onePageNum = onePageNum;
		this.url=url;
		this.init(count, pageNow);
	}
	
	
	public String getPageList(){
		
		String pageList = "<ul class='paginList'>";
		pageList += this.prev();
		
		if(pageTotal<(2*LR+1)){
			//循环所有页数
			
			for(int i=1 ; i<=pageTotal ;i++){
				if(pageNow==i){
					pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
				}else{
					pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
				}	
			}
	
		}else{
			
			if(pageNow<=(LR+1)){
				for(int i=1 ; i<=(LR*2+1) ;i++){
					if(pageNow==i){
						pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
					}else{
						pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
					}
				}
				pageList += "<li class='paginItem more'><a href='javascript:;'>...</a></li>";
				pageList += "<li class='paginItem'><a href='"+url+"pageNow="+pageTotal+"'>"+pageTotal+"</a></li>";
			}else{
				//分页数大于 定义的左右页数
				//两种情况
				//1.当前页+LR+1<总页数   ...在后
				//2.当前页+LR+1>总页数   ...在前
				if(pageNow+LR+1<pageTotal){
					//...在后
					for(int i=(pageNow-LR) ; i<=pageNow ;i++){
						if(pageNow==i){
							pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
						}else{
							pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
						}
					}
					
					for(int i=(pageNow+1) ; i<=(pageNow+LR) ;i++){
						if(pageNow==i){
							pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
						}else{
							pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
						}
					}
					pageList += "<li class='paginItem more'><a href='javascript:;'>...</a></li>";
					pageList += "<li class='paginItem'><a href='"+url+"pageNow="+pageTotal+"'>"+pageTotal+"</a></li>";
				}else{
					//...在前
					//两种情况
					//1.pageNow+LR<=pageTotal 
					//2.相反
					pageList += "<li class='paginItem'><a href='"+url+"pageNow=1'>"+1+"</a></li>";
					pageList += "<li class='paginItem more'><a href='javascript:;'>...</a></li>";
					if(pageNow+LR<=pageTotal){
						for(int i=(pageNow-LR) ; i<=pageNow ;i++){
							if(pageNow==i){
								pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}else{
								pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}
						}
						
						for(int i=(pageNow+1) ; i<=(pageNow+LR) ;i++){
							if(pageNow==i){
								pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}else{
								pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}
						}
					}else{
						//循环到尾页
						for(int i=pageTotal-(2*LR) ; i<=pageTotal ;i++){
							if(pageNow==i){
								pageList += "<li class='paginItem current'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}else{
								pageList += "<li class='paginItem'><a href='"+url+"pageNow="+i+"'>"+i+"</a></li>";	
							}
						}
						
					}
					
				}
				
			}
			
		}
		
		pageList += this.next();
		pageList += "<ul>";
		return  pageList;
	}
	
	private String prev(){
		if(pageNow==1){
			return "<li class='paginItem'><a href='javascript:;' title='已经是第一页啦!' ><span class='pagepre'></span></a></li>";
		}else{
			return "<li class='paginItem'><a href='"+url+"pageNow="+(pageNow-1)+"' ><span class='pagepre'></span></a></li>";
		}
	}
	
	private String next(){
		if(pageNow==pageTotal){
			return "<li class='paginItem'><a href='javascript:;' title='已经是最后一页啦!'  ><span class='pagenxt'></span></a></li>";
		}else{
			return "<li class='paginItem'><a href='"+url+"pageNow="+(pageNow+1)+"'><span class='pagenxt'></span></a></li>";
		}
	}
	
	public String getPageInfo(){
		return "共<i class=\"blue\"> "+count+ " </i>条记录，当前显示第&nbsp;<i class=\"blue\" > "+pageNow +" &nbsp;</i>页";
		//return "共有 "+count+ " 条记录,当前显示第 "+pageNow +" 页";
	}
	//构造函数 初始化
	private void  init(int count,String pageNow){
		//初始化参数
					this.count = count;
					//计算当前总页数
					if(count%onePageNum==0){
						pageTotal = count/onePageNum;
					}else{
						pageTotal = count/onePageNum+1;
					}
					//当前第几页容错处理
					if(pageNow!=null){
						if(Tools.isNumeric(pageNow)){
							this.pageNow = Integer.parseInt(pageNow);
							if(this.pageNow>=this.pageTotal){
								this.pageNow = this.pageTotal;
							}else if(this.pageNow<1){
								this.pageNow=1;
							}
						}else{
							this.pageNow = 1;
						}
					}else{
						this.pageNow = 1;
					}
		}
	
	/**
	 * 获取limit
	 * @return
	 */
	public String getLimit(){
		//计算分页所需的limit
		if(this.pageNow==0) this.pageNow=1;
		int limit = (this.pageNow-1)*onePageNum;
		return "limit "+limit+" , "+this.onePageNum;
	}
	
}
